from typing import List, Dict, Any
from .record_iterator import MedicalRecordIterator
from .extractor import MedicalRecordExtractor
from .quality_control import QualityControl
from .result_output import ResultOutput, AuditResult
from .medical_record import MedicalRecord


class MedicalRecordAuditSystem:
    """
    电子病历批量审核系统
    基于迭代器模式实现，支持分批处理大量病历数据
    """
    
    def __init__(self, batch_size: int = 10, output_file: str = "audit_results.csv"):
        """
        初始化审核系统
        
        Args:
            batch_size (int): 每批次处理的病历数量
            output_file (str): 审核结果输出文件路径
        """
        self.batch_size = batch_size
        self.output_file = output_file
        self.extractor = MedicalRecordExtractor()
        self.quality_control = QualityControl()
        self.result_output = ResultOutput(output_file)
    
    def audit_records(self, records_data: List[Dict[str, Any]]) -> None:
        """
        批量审核病历记录
        
        Args:
            records_data (List[Dict[str, Any]]): 病历数据列表
        """
        # 创建迭代器
        iterator = MedicalRecordIterator(records_data, self.batch_size)
        
        # 逐批处理病历数据
        for batch in iterator:
            audit_results = []
            
            # 处理每条病历记录
            for record in batch:
                # 进行质控校验
                is_valid, errors = self.quality_control.validate_record(record)
                
                # 创建审核结果
                audit_result = AuditResult(record.patient_id, is_valid, errors)
                audit_results.append(audit_result)
            
            # 将审核结果写入文件
            self.result_output.write_results(audit_results)
    
    def get_statistics(self):
        """
        获取审核统计信息
        
        Returns:
            tuple: (总审核量, 通过量, 通过率)
        """
        return self.result_output.get_statistics()
    
    def print_statistics(self):
        """
        打印审核统计信息
        """
        self.result_output.print_statistics()